package com.simpleaccount.Graph;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.simpleaccount.Common.Utils;
import com.simpleaccount.Common.DateUtils;
import com.simpleaccount.Db.DbUtils;
import com.simpleaccount.Entity.Category;
import com.simpleaccount.Entity.Entry;
import com.simpleaccount.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.view.PieChartView;

/**
 * Created by Administrator on 2017/7/12.
 */

public class PieChartFragment extends Fragment {
    @Bind(R.id.pcv_1)
    PieChartView pcv1;
    @Bind(R.id.lv_chart)
    ListView lvChart;
    private List<Category> categoryList;
    private HashMap<String,Float> rawData;
    private PieChartData data;
    private Bundle bundle;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bundle=getArguments();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        categoryList = getCategoryList();
        getRawData(bundle.getLong("date"));
        View view = inflater.inflate(R.layout.fragment_piechart, container, false);
        ButterKnife.bind(this, view);
        for(Category cate:categoryList){
            if(rawData.get(cate.getName())!=0f){
                initPieChartView();
                lvChart.setAdapter(new MyListViewAdapter(getContext(),rawData,getCategoryList()));
                Utils.setListViewHeightBasedOnChildren(lvChart);
                inflater.inflate(R.layout.fragment_graph,null,false).findViewById(R.id.tv_hint).setVisibility(View.INVISIBLE);
                return view;
            }
        }
        view.setVisibility(View.INVISIBLE);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public static PieChartFragment newInstance(long date,int stat) {
        PieChartFragment myFragment = new PieChartFragment();
        Bundle args = new Bundle();
        args.putLong("date", date);
        args.putInt("stat",stat);
        myFragment.setArguments(args);
        return myFragment;
    }

    public void initPieChartView(){
        pcv1.setZoomEnabled(false);
        pcv1.setCircleFillRatio(0.8f);
        initPieChartData();
    }

    private void initPieChartData() {
        List<SliceValue> values = new ArrayList<>();
        if(0==rawData.size()){
            return;
        }
        for(int i = 0; i< categoryList.size(); i++){
            Float f=rawData.get(categoryList.get(i).getName());
            if(f!=0f){
                SliceValue sliceValue = new SliceValue(null==f?0:f, categoryList.get(i).getColor());
                sliceValue.setLabel(categoryList.get(i).getName()+" \n"+String.format("%.2f",sliceValue.getValue()));
                values.add(sliceValue);
            }

        }
        data = new PieChartData(values);
        data.setHasLabelsOnlyForSelected(false);//不用点击显示占的百分比
        data.setHasCenterCircle(true);;//是否是环形显示
        data.setCenterCircleScale(0.7f);////设置环形的大小级别
        if(bundle.getInt("stat")==1){
            data.setCenterText1("总收入");
        }else if(bundle.getInt("stat")==0){
            data.setCenterText1("总支出");
        }
        pcv1.setPieChartData(data);


    }
//    private PieChartOnValueSelectListener selectListener = new PieChartOnValueSelectListener() {
//
//        @Override
//        public void onValueDeselected() {
//            // TODO Auto-generated method stub
//
//        }
//
//        @Override
//        public void onValueSelected(int arg0, SliceValue value) {
////            //选择对应图形后，在中间部分显示相应信息
////            data.setCenterText1(String.format("%.2f",value.getValue()));
////            data.setCenterText1Color(value.getColor());
////            data.setCenterText1FontSize(5);
////            data.setCenterText2(calPercent(arg0));
////            data.setCenterText2Color(value.getColor());
////            data.setCenterText2FontSize(12);
//        }
//    };
    private List<Category> getCategoryList() {
        return DbUtils.getCategory(bundle.getInt("stat"));
    }

    private void getRawData(long date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);

        List<Entry> entries = DbUtils.queryByDate(DateUtils.getTimesMonthmorning(calendar), DateUtils.getTimesMonthnight(calendar));
        rawData = new HashMap<>();
        for(Category cate: categoryList){
            rawData.put(cate.getName(),0f);
        }
        for (Entry e : entries) {
            String cate = e.getCategory();
            Float sum =  rawData.get(cate);
            if(sum!=null){
                rawData.put(cate, (float) e.getMoney() + sum);
            }
        }
    }

    private String calPercent(int i) {
        String result = "";
        int sum = 0;
        for (int i1 = 0; i1< categoryList.size(); i1++) {
            sum += rawData.get(categoryList.get(i1).getName());
        }
        result = String.format("%.2f",(rawData.get(categoryList.get(i).getName())/sum*100)) + "%";
        return result;
    }
}
